package com;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Jayesh
 */
public class initDB extends HttpServlet {

    @Override
    public void init() {

       // System.out.println("\nReading DB connection properties from global.properties: ");
        
        
        try {
            Properties dbProps = new Properties();

            //The forward slash "/" in front of in_filename will ensure that
            //no package names are prepended to the filename when the Classloader
            //search for the file in the classpath
            InputStream is = getClass().getResourceAsStream("/global.properties");
            if (null == is) {
                throw new Exception("Can't locate file: global.properties");
            }
            try {
                dbProps.load(is);//this may throw IOException
                //System.out.println(" Success");
                Root.dbURL = dbProps.getProperty("DBUrl");
                Root.dbName = dbProps.getProperty("DBName");
                Root.dbUserName = dbProps.getProperty("DBUser");
                Root.dbPwd = dbProps.getProperty("DBPassword");
                Root.dbDriver = dbProps.getProperty("DBDriver");

            } catch (Exception e) {
                System.out.println(" Error: " + e.toString());
                System.err.println("Properties loading failed in AppConfig");
                throw new Exception("Can't locate file: global.properties: ");
            }
        } catch (Exception e) {
            System.out.println(" Error: " + e.toString());
        } finally {
        }
    }

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String outstring = "<big style=\"color: rgb(117, 17, 41);\"><big>Database Connection Properties</big></big><br><br><table "
                + "style=\"text-align: left; background-color: rgb(218, 240, 235); width: 420px; height: 252px;\" border=\"0\" "
                        + "cellpadding=\"5\" cellspacing=\"5\"><tbody><tr><td style=\"font-weight: bold; color: rgb(53, 80, 53); "
                        + "text-align: left;\" valign=\"middle\">DB URL</td><td style=\"vertical-align: middle; text-align: left; "
                        + "color: rgb(53, 80, 53);\">" + Root.dbURL + "</td></tr><tr><td style=\"font-weight: bold;"
                        +"color: rgb(53, 80, 53); text-align: left;\" valign=\"middle\">DB Name</td><td style=\"text-align: left;"
                        +"color: rgb(53, 80, 53);\" valign=\"middle\">" + Root.dbName + "</td></tr><tr><td style=\"font-weight: bold; color: "
                            +"rgb(53, 80, 53); text-align: left;\" valign=\"middle\">DB Username</td><td style=\"text-align: left; "
                            +"color: rgb(53, 80, 53);\" valign=\"middle\">" + Root.dbUserName + "</td></tr><tr><td style=\"font-weight: bold; color: "
                            +"rgb(53, 80, 53); text-align: left;\" valign=\"middle\">DB Password</td><td style=\"text-align: left; "
                            +"color: rgb(53, 80, 53);\" valign=\"middle\">" + Root.dbPwd + "</td></tr><tr><td style=\"font-weight: bold; "
                            +"color: rgb(53, 80, 53); text-align: left;\" valign=\"middle\">DB Driver</td><td style=\"vertical-align: middle; "
                            +"text-align: left; color: rgb(53, 80, 53);\">" + Root.dbDriver + "</td></tr></tbody></table>";
        try {
            /* Display db parameters */
            out.println("<br/>"+outstring);
             
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
